<template>
    <div id="mymessage">
        <div class="mark" @click="allCheck"><img src="../assets/icon_sign.png">全部标记为已读</div>
        <div v-for="(content,index) in comments" :key="index.a" class="main">
            <div class="box" @click="markRead(content.commentId)">
                <div class="line-one clearfix">
                    <div class="answerYou lf">{{content.user.nickname}}回复你</div>
                    <div class="time gt">{{formattime(content.create_time)}}</div>
                </div>
                <div class="line-two" @click="ind=index">
                    <span class="redPoint" v-show="!content.isRead"></span>
                    <div class="content">
                        {{content.content}}
                    </div>
                </div>
                <div class="line-three">
                    <router-link :to="{path:'/articleDetail',query:{newsId:content.articleId._id}}" @click="check(content.commentId)">
                    来自：{{content.articleId.title}}
                    </router-link>
                </div>
            </div>
        </div>
        <infinite-loading :on-infinite="onInfinite" ref="infiniteLoading">
            <span slot="no-more">
                There is no more News :(
            </span>
        </infinite-loading>
    </div>
</template>
<script>
import {getMymsg,msgCheck} from '../api/example';
import moment from "moment";
import InfiniteLoading from "vue-infinite-loading";
export default {
    name:'mymessage',
    data(){
        return{
            page:1,
            read:false,
            comments:[],
            newpage:'',
            ind:''
        }
    },
    components:{        
        InfiniteLoading
    },
    created:function(){
        this.getMsg();
    },
    methods:{
        getMsg(){
            getMymsg(this.page).then(res =>{
                if (res.data.code === "success") {                        
                    console.log(res.data);
                    // this.comments = this.comments.concat(res.data.data.commnets);
                    this.newpage = Math.ceil(res.data.data.counts/5);
                } else {
                    alert(res.data.message);
                }
            })
        },
        onInfinite() {
            getMymsg(this.page).then(res => {
                setTimeout(() => {
                    if (res.data.code === "success") {                        
                        console.log(res.data);
                        // this.comments = res.data.data.commnets;
                        this.comments = this.comments.concat(res.data.data.commnets);
                    } else {
                        alert(res.data.message);
                    }

                    if (Math.ceil(res.data.data.counts / 4) === this.page) {
                        this.$refs.infiniteLoading.$emit("$InfiniteLoading:complete");
                    } else {
                        this.$refs.infiniteLoading.$emit("$InfiniteLoading:loaded");
                        this.page++;
                    }
                }, 1000);
            });
        },
        loadMsg(){
            console.log(this.newpage)
            getMymsg(this.newpage).then(res =>{
                if (res.data.code === "success") {                        
                    console.log(res.data);
                    this.comments = this.comments.concat(res.data.data.commnets);
                    this.newpage = Math.ceil(res.data.counts/5);
                } else {
                    alert(res.data.message);
                }
            })
        },

        // 标记为已读
        markRead(commentId){
            msgCheck(commentId).then(res =>{
                if(res.data.code === 'success'){
                    console.log(res.data);
                    alert("标记为已读")
                    $('.redPoint').eq(this.ind).css("display","none");
                }
            })
        },
        // 全部消息已读
        allCheck(){
            msgCheck().then(res =>{
                if(res.data.code === 'success'){
                     $('.redPoint').css("display","none");
                     alert('已全部标记为已读')
                    console.log(res.data);
                }
            })
        },
        // 消息已读
        check(commentId){
            msgCheck(commentId).then(res =>{
                if(res.data.code === 'success'){
                    console.log(res.data);
                }
            })
        },  
        formattime(time) {
            return moment(time).format("YYYY-MM-DD HH:mm:ss");
        },
    }
}
</script>
<style scoped>
    #mymessage{
        position: relative;
        padding:0 30px;
    }
    .mark{
        position: absolute;
        right:-30px;
        top:-33px;
        cursor: pointer;
    }
    .main{
        font-size: 16px;
        color: #6F6F6F;
        padding:30px 0;
        border-bottom: 1px solid #DCDCDC;
    }
    .box{
        cursor: pointer;
    }
    .box .line-one{
        margin-bottom:15px;
    }
    .box .line-two{
        position: relative;
        color:#333;
        margin-bottom: 22px;
    }
    .box:hover .redPoint::before{
        background: red;
    }
    .box .redPoint::before{
        content: '';
        position: absolute;
        top:6px;
        left:-25px;
        display: inline-block;
        width:10px;
        height:10px;
        border-radius: 50%;
        background: #FF8000;
    }
    .main .box .Point::before{
        content: '';
        position: absolute;
        top:6px;
        left:-25px;
        display: inline-block;
        width:10px;
        height:10px;
        border-radius: 50%;
        background: transparent;
    }
</style>
